const listData = [
    {
        menuId: 1,
        name: "组织中台",
        assignType: 2,
        checked: false
    },
    {
        name: "岗位管理",
        menuId: 22,
        parentId: 1,
        assignType: 2,
        checked: false
    },
    {
        name: "称谓体系管理",
        menuId: 23,
        parentId: 22,
        assignType: 2,
        checked: false
    },
    {
        name: "部门岗位管理",
        menuId: 24,
        parentId: 22,
        assignType: 2,
        checked: false
    },
    {
        name: "员工管理",
        menuId: 2,
        parentId: 1,
        assignType: 2,
        checked: false
    },
    {
        name: "入职管理",
        menuId: 3,
        parentId: 2,
        assignType: 2,
        checked: false
    },
    {
        name: "离职管理",
        menuId: 4,
        parentId: 2,
        assignType: 2,
        checked: false
    },
    {
        name: "待录用",
        menuId: 5,
        parentId: 3,
        assignType: 2,
        checked: false
    },
    {
        name: "待发offer",
        menuId: 6,
        parentId: 3,
        assignType: 2,
        checked: false
    },
    {
        name: "固定资产",
        menuId: 7,
        assignType: 2,
        checked: false
    },
    {
        name: "资产列表",
        menuId: 8,
        parentId: 7,
        assignType: 2,
        checked: false
    },
    {
        name: "日清",
        menuId: 9,
        assignType: 1,
        checked: true
    },
    {
        name: "日清",
        menuId: 10,
        parentId: 9,
        assignType: 1,
        checked: true
    },
    {
        name: "核心能力",
        menuId: 11,
        parentId: 9,
        assignType: 1,
        checked: true
    },
    {
        name: "车辆预约",
        menuId: 12,
        assignType: 2,
        checked: true
    },
    {
        name: "车辆管理",
        menuId: 13,
        parentId: 12,
        assignType: 2,
        checked: true
    },
    {
        name: "CEO信箱",
        menuId: 14,
        assignType: 3,
        checked: false
    },
    {
        name: "WMS库存管理",
        menuId: 15,
        assignType: 3,
        checked: true
    },
    {
        name: "单据入库",
        menuId: 16,
        parentId: 15,
        assignType: 3,
        checked: true
    }
];

function listToTree(array, parentNodeId = "parentId", nodeId = "menuId") {
    let arrayData = JSON.parse(JSON.stringify(array));
    let treeData = [];
    let treeMap = {};
    for (let i = 0, len = arrayData.length; i < len; i++) {
        let parent = arrayData[i];
        let children = [];
        let filterRepeatArr = [];
        if (!parent.level) {
            parent.level = 1;
            parent.expand = true;
            parent.fullCode = parent[nodeId] + "/";
        }
        if (!treeMap[`${arrayData[i]["fullCode"]}`]) {
            treeMap[`${arrayData[i]["fullCode"]}`] = arrayData[i];
        }
        for (let j = 0; j < len; j++) {
            let child = arrayData[j];
            if (
                parent[nodeId] &&
                child[parentNodeId] &&
                parent[nodeId] === child[parentNodeId]
            ) {
                child.level = parent.level + 1;
                child.hasParent = true;
                if (!filterRepeatArr.includes(child[nodeId])) {
                    child.fullCode = parent.fullCode + `${child[nodeId]}/`;
                    filterRepeatArr.push(child[nodeId]);
                    children.push(child);
                }
            }
        }
        if (children.length) {
            parent.children = children;
        }
    }
    for (let i = 0, len = arrayData.length; i < len; i++) {
        if (!arrayData[i].hasParent) {
            treeData.push(arrayData[i]);
        }
    }
    this.treeMap = treeMap;
    return treeData;
}

// console.log(listToTree(listData));

function list2Tree(arr, parentNodeId = 'parentId', nodeId = "menuId") {
    arr = JSON.parse(JSON.stringify(arr))
    let treeData = []
    let repeatArr = []

    for (let i = 0, len = arr.length; i < len; i++) {
        let parent = arr[i]
        let children = []
        if (!parent.level) {
            parent.level = 1
        }

        for (let j = 0; j < len; j++) {
            let child = arr[j]

            if (parent[nodeId] && child[parentNodeId] && parent[nodeId] === child[parentNodeId]) {
                child.hasParent = true
                child.level = parent.level + 1
                // 这里需要判断一下，是否被遍历过了， 如果被遍历过了，就不处理
                if (!repeatArr.includes(child[nodeId])) {
                    children.push(child)
                    repeatArr.push(child[nodeId])
                }
            }
        }
        children && children.length && (parent.child = children)
        // parent.child = children

        if (parent.level === 1) {
            treeData.push(parent)
        }

        // 或者使用下面的方法
        for (const item of arr) {
            if (!item.hasParent) {
                treeData.push(item)
            }
        }
    }
    console.log(treeData)
}

// list2Tree(listData)


function list2Tree1(arr, parentId = 'parentNodeId', nodeId = 'nodeId') {
    arr = JSON.parse(JSON.stringify(arr))
    let treeData = []
    let repeatArr = []

    for (let item of arr) {
        let parent = item
        let children = []
        // 记录当前遍历的是否有层级，如果没有就没有遍历， 就认为其是父亲
        // 如果后来的是
        if (!parent.level) {
            parent.level = 1
        }

        for (let value of arr) {
            if (value[parentId] && parent[nodeId] && value[parentId] === parent[parentId]) {
                child.hasParent = true;
                child.leve += 1
                if (!repeatArr.includes(child)) {
                    children.push(value)
                    // repeat只记录被认领的孩子
                    repeatArr.push(child[nodeId])
                }

            }
            children && children.length && (parent.children = children)
        }

        if (parent.level === 1) {
            treeData.push(parent)
        }

    }
    return treeData
}




